//
//  CMMyFeedBackCtrl.m
//  CreditManger
//
//  Created by chaiyanan on 15/11/2.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CMMyFeedBackCtrl.h"
#import "CheckValid.h"
#import "LCCustomerActionSheet.h"

#import "CMUserFeedbackParam.h"
#import "CMUserFeedbackParser.h"
#import "CMLoginManager.h"

#define Key_FeedPlaceHolderText @"欢迎吐槽，我们会尽全力改进~"

@interface CMMyFeedBackCtrl ()
<UITextViewDelegate,
UINavigationControllerDelegate,
UIImagePickerControllerDelegate
>

@property (nonatomic, weak) IBOutlet UITextView *feedTV;
@property (nonatomic, weak) IBOutlet UIButton *imageBtn1;
@property (nonatomic, weak) IBOutlet UIButton *imageBtn2;
@property (nonatomic, weak) IBOutlet UIButton *imageBtn3;
@property (nonatomic, weak) IBOutlet UIButton *imageBtn4;
@property (nonatomic) NSInteger currentImageBtn;

@end

@implementation CMMyFeedBackCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadEmptyTextView];
    [self customLayout];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)customLayout
{
    self.imageBtn1.layer.borderColor = [LCCommon getColor:@"cccccc"].CGColor;
    self.imageBtn1.backgroundColor = [LCCommon getColor:@"eeeded"];
    self.imageBtn1.layer.borderWidth = .5f;
    self.imageBtn1.tag = 2001;
    
    self.imageBtn2.layer.borderColor = [LCCommon getColor:@"cccccc"].CGColor;
    self.imageBtn2.backgroundColor = [LCCommon getColor:@"eeeded"];
    self.imageBtn2.layer.borderWidth = .5f;
    self.imageBtn2.tag = 2002;
    
    self.imageBtn3.layer.borderColor = [LCCommon getColor:@"cccccc"].CGColor;
    self.imageBtn3.backgroundColor = [LCCommon getColor:@"eeeded"];
    self.imageBtn3.layer.borderWidth = .5f;
    self.imageBtn3.tag = 2003;
    
    self.imageBtn1.enabled = NO;
    self.imageBtn2.enabled = NO;
    self.imageBtn3.enabled = NO;
    
    self.imageBtn4.layer.borderColor = [LCCommon getColor:@"cccccc"].CGColor;
    self.imageBtn4.layer.borderWidth = .5f;
    self.imageBtn4.tag = 2004;
    
}

- (void)loadEmptyTextView
{
    self.feedTV.text = Key_FeedPlaceHolderText;
    self.feedTV.textColor = [LCCommon getColor:@"cacaca"];

}

- (void)loadEditedTextView
{
    self.feedTV.text = @"";
    self.feedTV.textColor = [UIColor darkGrayColor];
}

- (void)clickToSend:(UIButton *)sender
{
    if ([self.feedTV.text isEqualToString:Key_FeedPlaceHolderText]||[CheckValid checkStrIsEmpty:self.feedTV.text])
    {
       
        DISSMISS_ERRView(@"请输入反馈内容",[[[UIApplication sharedApplication] windows] lastObject]);
    }
    else
    {
        [self.feedTV resignFirstResponder];
        [self requestToFeedback];
    }
}

- (void)tapDone:(UITapGestureRecognizer *)recongnizer
{
    [self.view endEditing:YES];
}

- (void)clickRightBtn
{
    [self clickToSend:nil];
}

- (IBAction)clickToMakePhoto:(id)sender
{
    [self.feedTV resignFirstResponder];
    
    if ([self.imageBtn1 imageForState:UIControlStateNormal]&&[self.imageBtn2 imageForState:UIControlStateNormal]&&[self.imageBtn3 imageForState:UIControlStateNormal]&&[self.imageBtn4 imageForState:UIControlStateNormal])
    {
        self.imageBtn4.enabled = NO;
        return;
    }
    
//    UIButton *btn = (UIButton *)sender;
    LLog(@"1:%@,2:%@,3:%@,4:%@",[self.imageBtn1 imageForState:UIControlStateNormal],[self.imageBtn2 imageForState:UIControlStateNormal],[self.imageBtn3 imageForState:UIControlStateNormal],[self.imageBtn4 imageForState:UIControlStateNormal]);
    
    [[[LCCustomerActionSheet sharedLCCustomerActionSheet] initWithTitle:nil cancelButtonTitle:@"取消" cancelButtonAction:nil otherButtonTitles:@[@"拍照",@"相册选取"] otherButtonAction:^(NSInteger index,LCCustomerActionSheet *actionSheet)
      {
          if (index == 0)
          {
              [self openImagePickerWithSourcheType:UIImagePickerControllerSourceTypeCamera];
              
          }
          else
          {
              [self openImagePickerWithSourcheType:UIImagePickerControllerSourceTypePhotoLibrary];
          }
          [actionSheet removeFromSuperview];
          
      }] showInView];
}

- (IBAction)clickToEndEditing
{
    [self.feedTV resignFirstResponder];
}

#pragma mark -
#pragma mark - photo privateMethod
- (void)openImagePickerWithSourcheType:(UIImagePickerControllerSourceType)sourceType
{
    if (sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            [[[UIAlertView alloc]initWithTitle:@"提示" message:@"该设备不支持相机" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
            return;
        }
    }
    
    CMAppDelegate.isFromCamera = YES;
    UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    picker.sourceType = sourceType;
    picker.delegate = self;
    picker.allowsEditing = YES;
    
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark -
#pragma mark - imagePickerDelegate

- ( void )imagePickerController:( UIImagePickerController *)picker didFinishPickingMediaWithInfo:( NSDictionary *)info
{
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    
    if (![self.imageBtn1 imageForState:UIControlStateNormal])
    {
        [self.imageBtn1 setImage:image forState:UIControlStateNormal];
        [self.imageBtn1 setImage:image forState:UIControlStateDisabled];
    }
    else if (![self.imageBtn2 imageForState:UIControlStateNormal])
    {
        [self.imageBtn2 setImage:image forState:UIControlStateNormal];
        [self.imageBtn2 setImage:image forState:UIControlStateDisabled];
        
    }
    else if (![self.imageBtn3 imageForState:UIControlStateNormal])
    {
        [self.imageBtn3 setImage:image forState:UIControlStateNormal];
        [self.imageBtn3 setImage:image forState:UIControlStateDisabled];
    }
    else
    {
        [self.imageBtn4 setImage:image forState:UIControlStateNormal];
        [self.imageBtn4 setImage:image forState:UIControlStateDisabled];
    }

    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- ( void )imagePickerControllerDidCancel:( UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark
#pragma mark - textViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:Key_FeedPlaceHolderText])
    {
        [self loadEditedTextView];
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:Key_FeedPlaceHolderText]||[textView.text isEqualToString:@""])
    {
        [self loadEmptyTextView];
    }
    return YES;
}

#pragma mark
#pragma mark - net method

- (void)requestToFeedback
{
    CMUserFeedbackParam *param = [[CMUserFeedbackParam alloc]init];
    param.uid = [CMLoginManager sharedCMLoginManager].currentUserInfo.uid;
    param.token = [CMLoginManager sharedCMLoginManager].currentUserInfo.token;
    param.content = self.feedTV.text;
    if ([self.imageBtn1 imageForState:UIControlStateNormal])
    {
        param.file1 = UIImageJPEGRepresentation([self.imageBtn1 imageForState:UIControlStateNormal], 0.3);
    }
    if ([self.imageBtn2 imageForState:UIControlStateNormal])
    {
        param.file2 = UIImageJPEGRepresentation([self.imageBtn2 imageForState:UIControlStateNormal], 0.3);
    }
    if ([self.imageBtn3 imageForState:UIControlStateNormal])
    {
        param.file3 = UIImageJPEGRepresentation([self.imageBtn3 imageForState:UIControlStateNormal], 0.3);
    }
    if ([self.imageBtn4 imageForState:UIControlStateNormal])
    {
        param.file4 = UIImageJPEGRepresentation([self.imageBtn4 imageForState:UIControlStateNormal], 0.3);
    }
    CMUserFeedbackParser *parser = [[CMUserFeedbackParser alloc]init];
    [NetCenter postDataFromURL:Method_userFeedback params:param parser:parser result:^(ParserResult *myResult) {
        if (myResult.code == 0)
        {
            DISSMISS_SUC(myResult.message);
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
    } failure:^(NSError *error) {
        
        DISSMISS_ERR([error description]);
        
    }
    progressBlock:^(float progress)
    {
        LLog(@"progress---------------%f",progress);
    }];
}


@end
